package net.mingsoft.mdiy.util;

import net.mingsoft.mdiy.entity.*;
import java.util.*;
import cn.hutool.json.*;
import org.apache.commons.lang3.*;
import net.mingsoft.mdiy.biz.*;
import net.mingsoft.basic.util.*;
import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.core.conditions.*;
import cn.hutool.core.map.*;

public class ConfigUtil
{
    public static Map<String, ConfigEntity> CONFIG_HASH_MAP;
    
    public static String getString(final String configName, final String key) {
        final Object object = getObject(configName, key);
        if (object == null) {
            return "";
        }
        return object.toString();
    }
    
    public static String getString(final String configName, final String key, final String defaultValue) {
        final Object object = getObject(configName, key);
        if (object == null) {
            return defaultValue;
        }
        return object.toString();
    }
    
    public static Boolean getBoolean(final String configName, final String key) {
        final Object object = getObject(configName, key);
        if (object == null) {
            return Boolean.FALSE;
        }
        return BooleanUtils.toBoolean(object.toString());
    }
    
    public static Boolean getBoolean(final String configName, final String key, final Boolean defaultValue) {
        final Object object = getObject(configName, key);
        if (object == null) {
            return defaultValue;
        }
        return BooleanUtils.toBoolean(object.toString());
    }
    
    public static int getInt(final String configName, final String key) {
        final Object object = getObject(configName, key);
        if (object == null) {
            return 0;
        }
        return Integer.parseInt(object.toString());
    }
    
    public static int getInt(final String configName, final String key, final int defaultValue) {
        final Object object = getObject(configName, key);
        if (object == null) {
            return defaultValue;
        }
        return Integer.parseInt(object.toString());
    }
    
    public static Object getObject(final String configName, final String key) {
        final ConfigEntity configEntity = getEntity(configName);
        if (configEntity == null) {
            return null;
        }
        final HashMap map = (HashMap)JSONUtil.toBean(configEntity.getConfigData(), (Class)HashMap.class);
        if (map != null) {
            return map.get(key);
        }
        return null;
    }
    
    public static Map getMap(final String configName) {
        final ConfigEntity configEntity = getEntity(configName);
        if (configEntity == null || StringUtils.isEmpty((CharSequence)configEntity.getConfigData())) {
            return null;
        }
        return (Map)JSONUtil.toBean(configEntity.getConfigData(), (Class)HashMap.class);
    }
    
    public static ConfigEntity getEntity(final String configName) {
        if (StringUtils.isEmpty((CharSequence)configName)) {
            return null;
        }
        if (ConfigUtil.CONFIG_HASH_MAP.get(configName) != null) {
            return ConfigUtil.CONFIG_HASH_MAP.get(configName);
        }
        final IConfigBiz configBiz = (IConfigBiz)SpringUtil.getBean((Class)IConfigBiz.class);
        ConfigEntity configEntity = new ConfigEntity();
        configEntity.setConfigName(configName);
        configEntity = (ConfigEntity)configBiz.getOne((Wrapper)new QueryWrapper((Object)configEntity));
        if (configEntity == null) {
            return null;
        }
        ConfigUtil.CONFIG_HASH_MAP.put(configName, configEntity);
        return configEntity;
    }
    
    public static void removeEntity(final String configName) {
        if (StringUtils.isNotEmpty((CharSequence)configName) && ConfigUtil.CONFIG_HASH_MAP.get(configName) != null) {
            ConfigUtil.CONFIG_HASH_MAP.remove(configName);
        }
    }
    
    static {
        ConfigUtil.CONFIG_HASH_MAP = MapUtil.newHashMap();
    }
}
